package com.myway.ibe.command;

import com.google.common.base.Preconditions;
import com.myway.ibe.exception.IBEException;
import com.travelsky.ibe.client.pnr.BookPassenger;
import com.travelsky.ibe.client.pnr.PnrManage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * Created by Administrator on 2018/1/15.
 * 修改旅客资料
 */
@Slf4j
public class ChangePaxInfoCommand extends BaseIBECommand {
    @Override
    public void execute(Map<String, Object> params) {
        DTOPassenger oldPassenger=(DTOPassenger)params.get("oldPassenger");
        DTOPassenger newPassenger=(DTOPassenger)params.get("newPassenger");
        String pnrno=(String)params.get("pnrno");
        String foid=(String)params.get("foid");
        boolean useBackupIP = params.containsKey("useBackupIP")?(Boolean)params.get("useBackupIP"):false;

        if (oldPassenger.getPnrValue() != null
                && StringUtils.isNotBlank(oldPassenger.getPnrValue().getPnrNo())){
            PnrManage manage=new PnrManage();
            configIBEClient(manage, airline, useBackupIP);
            BookPassenger oldpax = new BookPassenger();
            BookPassenger newpax = new BookPassenger();
            try {
                oldpax.setName(oldPassenger.getName());
                oldpax.setType(PNRUtils.convertBookPassengerType(oldPassenger.getPassengerType()));
                newpax.setName(newPassenger.getName());
                newpax.setType(PNRUtils.convertBookPassengerType(newPassenger.getPassengerType()));
                String result="";
                if(oldPassenger.getName().equals(newPassenger.getName()) && oldPassenger.getPassengerType().equals(newPassenger.getPassengerType()))
                {
                    //姓名相同，说明乘客相同，仅修改证件信息，用于支持PN
                    result= manage.changeId(pnrno, newpax, foid);
                }
                else{
                    result= manage.changePaxId(pnrno, oldpax, newpax, foid);
                }
                Preconditions.checkArgument("OK".equalsIgnoreCase(result),"Unexpect result while changing passenger name");
            }catch (Exception e) {
                log.error("error:{}",e.getMessage().toString());
                throw new IBEException(e.getMessage());
            }
        } else {
            log.info("Passenger " + oldPassenger.getName() + " does not belong to IBE PNR. Update to IBE is not required.");
        }
    }
}
